import { loginRegisterApi, type LoginDataState } from '@/api'
import { request } from '@/service/request'
import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useUserStore = defineStore('user', () => {
  // setup
  const phone = ref('')

  const userLogin = (data: LoginDataState) => {
    return new Promise<{ code: number; data: { token: string, refreshToken: string } }>(async (resolve, reject) => {
      const resp = await loginRegisterApi(data)
      if (resp.data.code === 0) {
        resolve(resp.data)
        phone.value = data.phone
      } else {
        reject(resp.data)
      }
    })
  }

  const getUserInfo = () => {
    return new Promise((resolve, reject) => {
      request({
        method: 'post',
        url: '/users/getUserInfo/costom'
      }).then((resp) => {
        if (resp.data.code === 0) {
          phone.value = resp.data.data.phone
          resolve(resp.data)
        }
      })
    })
  }

  const quitLogin = () => {
    return new Promise((resolve, reject) => {
       phone.value = '';
       resolve(null)
    }).catch((error) => error)
  }

  return { phone, userLogin, getUserInfo, quitLogin }
  
})
